package cn.wit.summit.chwan.models.dbmanager;

import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;

import cn.wit.summit.chwan.models.greendao.gen.DownLoadTaskTableDao;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.tools.downloadtool.DownLoadStatusConfig;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tangbl on 2017/12/2 0002.
 */

public class DownloadTaskManager {

    private static DownloadTaskManager downloadManager;

    private DownloadTaskManager() {
        mDownLoadTaskTableDao = MyApplication.getMyApplication().getmDownLoadTaskTableDao();
    }

    public static DownloadTaskManager getInstance() {
        if (downloadManager == null) downloadManager = new DownloadTaskManager();
        return downloadManager;
    }

    private DownLoadTaskTableDao mDownLoadTaskTableDao;

    public void insertDownload(DownLoadTaskTable task) {

        mDownLoadTaskTableDao.insertOrReplace(task);
    }

    public DownLoadTaskTable findByGameId(String gameId) {
        if (StringUtils.isEmpty(gameId))
            return null;
        List<DownLoadTaskTable> list = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).list();
        if (list!=null && list.size()>1) {
            for (int i=1;i<list.size();i++) {
                mDownLoadTaskTableDao.delete(list.get(i));
            }
        }
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).unique();
    }

    public List<DownLoadTaskTable> findAllGame() {
        return mDownLoadTaskTableDao.queryBuilder().list();
    }

    public List<DownLoadTaskTable> findAllDownloadFailed() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.DOWNLOAD_ERROR)).list();
    }

    public List<DownLoadTaskTable> findNotDownFinishNumber() {
        QueryBuilder qb = mDownLoadTaskTableDao.queryBuilder();
        qb.where(qb.or(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.DOWNLOADING), DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.UNZIP)));
//        qb.where(qb.and(DownLoadTaskTableDao.Properties.DownStatus.notEq(DownLoadStatusConfig.INSTALLED)
//                , DownLoadTaskTableDao.Properties.DownStatus.notEq(DownLoadStatusConfig.INSTALL_WAIT)));
        return qb.list();
    }

    public List<DownLoadTaskTable> findAllDownlodingGameByTime() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.notEq(DownLoadStatusConfig.INSTALLED)).orderDesc(DownLoadTaskTableDao.Properties.DownStartTime).list();
    }

    public List<DownLoadTaskTable> findAllDownlodedGame() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.INSTALLED)).list();
    }

    public List<DownLoadTaskTable> findAllDownlodedGamedownByTime() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.INSTALLED)).orderDesc(DownLoadTaskTableDao.Properties.DownStartTime).list();
    }

    public List<DownLoadTaskTable> findAllStayInstalledDownloadTask() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.INSTALL_WAIT)).list();
    }

    public List<DownLoadTaskTable> findAllUNZIPDownloadTask() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.UNZIP)).list();
    }

    public List<DownLoadTaskTable> findAllStayInstallingDownloadTask() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.INSTALLING)).list();
    }

    public List<DownLoadTaskTable> findAllInstalledDownloadTask() {
        return mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(DownLoadStatusConfig.INSTALLED)).list();
    }

    public void updateStatusBygameId(String gameId, int status) {
        LogUtils.e("DownStatues  updateStatusBygameId1 " + status);
        List<DownLoadTaskTable> list = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).list();
        if (list!=null && list.size()>1) {
            for (int i=1;i<list.size();i++) {
                mDownLoadTaskTableDao.delete(list.get(i));
            }
        }
        DownLoadTaskTable taskTable = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).unique();
        if (taskTable == null) {
            return;
        }
        LogUtils.e("DownStatues  updateStatusBygameId2 " + taskTable.getDownStatus());
        taskTable.setDownStatus(status);
        mDownLoadTaskTableDao.update(taskTable);
    }
    public void updateStatusAndSpeedBygameId(String gameId, int status,String speed) {
        LogUtils.e("DownStatues  updateStatusBygameId1 " + status);
        List<DownLoadTaskTable> list = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).list();
        if (list!=null && list.size()>1) {
            for (int i=1;i<list.size();i++) {
                mDownLoadTaskTableDao.delete(list.get(i));
            }
        }
        DownLoadTaskTable taskTable = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).unique();
        if (taskTable == null) {
            return;
        }
        LogUtils.e("DownStatues  updateStatusBygameId2 " + taskTable.getDownStatus());
        taskTable.setDownStatus(status);
        taskTable.setDownspeed(speed);
        mDownLoadTaskTableDao.update(taskTable);
    }

    public void updateUPStatusBygameId(String gameId, int upstatus) {
        List<DownLoadTaskTable> list = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).list();
        if (list!=null && list.size()>1) {
            for (int i=1;i<list.size();i++) {
                mDownLoadTaskTableDao.delete(list.get(i));
            }
        }
        DownLoadTaskTable taskTable = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.GameId.eq(gameId)).unique();
        if (taskTable == null) {
            return;
        }
        taskTable.setNotUpdateVersion(upstatus);
        mDownLoadTaskTableDao.update(taskTable);
    }

    public void updateDownloadTask(DownLoadTaskTable downLoadTaskTable) {
        mDownLoadTaskTableDao.insertOrReplace(downLoadTaskTable);
    }

    public void updateStatusByPackageName(String packageName, int status) {
        List<DownLoadTaskTable> list = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.PackageName.eq(packageName)).list();
        if (list!=null && list.size()>1) {
            for (int i=1;i<list.size();i++) {
                mDownLoadTaskTableDao.delete(list.get(i));
            }
        }
        DownLoadTaskTable taskTable = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.PackageName.eq(packageName)).unique();
        if (taskTable == null) {
            return;
        }
        taskTable.setDownStatus(status);
        mDownLoadTaskTableDao.update(taskTable);
    }

    public List<DownLoadTaskTable> findByStatus(String status) {
        List<DownLoadTaskTable> taskTables = mDownLoadTaskTableDao.queryBuilder().where(DownLoadTaskTableDao.Properties.DownStatus.eq(status)).list();
        if (taskTables != null) {
            return taskTables;
        } else {
            return new ArrayList<>();
        }
    }

    public boolean isExist(String downloadUrl) {
        return false;
    }

    public boolean deleteTaskTable(String gameId) {
        DownLoadTaskTable taskTable = findByGameId(gameId);
        if (taskTable != null) {
            mDownLoadTaskTableDao.delete(taskTable);
            return true;
        }
        return false;
    }

    public void delete(long id) {
        mDownLoadTaskTableDao.deleteByKey(id);
    }

    public DownLoadTaskTable getDownloadByUrl(String downloadUrl) {
        return null;
    }
}
